package com.javaee.ejb.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import com.javaee.ejb.entity.Activity;

/**
 * Session Bean implementation class ActivityManager
 */
@Stateless
public class ActivityManager implements ActivityManagerRemote {

	@PersistenceContext	
	EntityManager m_GestionnaireEntites;
	
    /**
     * Default constructor. 
     */
    public ActivityManager() {
        // TODO Auto-generated constructor stub
    }
    
  //Permet d'ajouter une activite
    @Override
    public void addActivity(Activity p_activity)
    {
    	try{
    		m_GestionnaireEntites.persist(p_activity);
    	}catch(PersistenceException e){
    		e.printStackTrace();
    	}
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<Activity> getAllActivities(int userId) {
		List<Activity> l_act = null;
		System.out.println("Employee ID: "+userId);
		try{
		l_act = (List<Activity>)m_GestionnaireEntites.createNamedQuery("FindAllActivities").setParameter("p_IdEmployee", userId).getResultList();
		}catch(Exception e){
			return null;
		}
		
		return l_act;
	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Activity> getProjectActivities(int projectId) {
		System.out.println("Project ID: "+projectId);
		return (List<Activity>)m_GestionnaireEntites.createNamedQuery("FindProjectActivities").setParameter("p_IdProject", projectId).getResultList();
	}

	@Override
	public List<Activity> getActivityByProject(int userId, int projectId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(int id) {
		Activity a = getActivityFromId(id);
		m_GestionnaireEntites.remove(a);	
	}

	@Override
	public Activity getActivityFromId(int id) {
		return (Activity)m_GestionnaireEntites.createNamedQuery("FindActivityById").setParameter("p_IdActivity", id).getSingleResult();
	}

}
